package ma.xin.shi;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import ma.xin.shi.table.TableManager;

public class CommDisplay {
	public static void display(Composite workArea) {
	    for (Control control : workArea.getChildren()) {
	        control.dispose();
	    }

	    Composite[] composites = addECUComposite(workArea);
	    CTabFolder tabFolder = (CTabFolder) composites[0];
	    Composite dataListComposite = composites[1];
	    Composite workAreaComposite = composites[2];

	    // add tab
	    addTabToFolder(tabFolder, "Someip", dataListComposite, workAreaComposite);
	    addTabToFolder(tabFolder, "DDS", dataListComposite, workAreaComposite);
	    addTabToFolder(tabFolder, "IPC", dataListComposite, workAreaComposite);

	    // set default load the first tab
	    if (tabFolder.getItemCount() > 0) {
	        updateContentAreas(dataListComposite, workAreaComposite, tabFolder.getItem(0).getText());
	    }

	    workArea.layout(true, true);
	}

	private static Composite[] addECUComposite(Composite parent) {
	    Composite mainArea = new Composite(parent, SWT.NONE);
	    GridLayout layout = new GridLayout(1, false);
	    layout.verticalSpacing = 0;
	    mainArea.setLayout(layout);

	    GridData mainAreaData = new GridData(SWT.FILL, SWT.FILL, true, true);
	    mainArea.setLayoutData(mainAreaData);

	    CTabFolder tabFolder = new CTabFolder(mainArea, SWT.BORDER | SWT.FLAT);
	    tabFolder.setSimple(false);
	    tabFolder.setBorderVisible(false);
	    tabFolder.setTabHeight(24);

	    GridData tabFolderLayoutData = new GridData(SWT.FILL, SWT.TOP, true, false);
	    tabFolderLayoutData.heightHint = 28;
	    tabFolder.setLayoutData(tabFolderLayoutData);

	    Composite contentArea = new Composite(mainArea, SWT.NONE);
	    GridLayout contentLayout = new GridLayout(2, false);
	    contentLayout.verticalSpacing = 0;
	    contentArea.setLayout(contentLayout);

	    contentArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

	    Composite dataListComposite = new Composite(contentArea, SWT.BORDER);
	    dataListComposite.setLayout(new GridLayout(1, false));

	    GridData dataListData = new GridData(SWT.LEFT, SWT.FILL, false, true);
	    dataListData.verticalIndent = 0;
	    dataListComposite.setLayoutData(dataListData);

	    Composite workAreaComposite = new Composite(contentArea, SWT.BORDER);
	    workAreaComposite.setLayout(new GridLayout(1, false));

	    GridData workAreaData = new GridData(SWT.FILL, SWT.FILL, true, true);
	    workAreaData.verticalIndent = 0;
	    workAreaComposite.setLayoutData(workAreaData);

	    return new Composite[]{tabFolder, dataListComposite, workAreaComposite};
	}

	private static void addTabToFolder(CTabFolder tabFolder, String tabName, Composite dataListComposite, Composite workAreaComposite) {
        CTabItem tabItem = new CTabItem(tabFolder, SWT.NONE);
        tabItem.setText(tabName);

        if (tabFolder.getData("listener-added") == null) {
            tabFolder.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    CTabItem selectedTab = tabFolder.getSelection();
                    String selectedTabName = selectedTab.getText();
                    updateContentAreas(dataListComposite, workAreaComposite, selectedTabName);
                }
            });

            tabFolder.setData("listener-added", Boolean.TRUE);
        }

        // set default tab
        tabFolder.setSelection(tabFolder.getItem(0));//tabFolder.setSelection(tabFolder.getItemCount() - 1);
    }

	private static void updateContentAreas(Composite dataListComposite, Composite workAreaComposite, String selectedTabName) {
	    for (Control child : dataListComposite.getChildren()) {
	        child.dispose();
	    }
	    for (Control child : workAreaComposite.getChildren()) {
	        child.dispose();
	    }

	    Label dataLabel = new Label(dataListComposite, SWT.CENTER);
	    Label workLabel = new Label(workAreaComposite, SWT.CENTER);

	    if ("Someip".equals(selectedTabName)) {
	        dataLabel.setText("CM 【Someip】IPC】data list");
	        workLabel.setText("CM 【Someip】workeare");
	    } else if ("DDS".equals(selectedTabName)) {
	        dataLabel.setText("CM 【DDS】data list");
	        workLabel.setText("CM 【DDS】workeare");
	    } else if ("IPC".equals(selectedTabName)) {
            dataLabel.setText("CM 【IPC】data list");
	        workLabel.setText("CM 【IPC】workeare");
	    } else {
	        dataLabel.setText("CM unknown tab：" + selectedTabName);
	        workLabel.setText("CM undefined Area");
	    }

	    dataListComposite.layout();
	    workAreaComposite.layout();
	}

    public static Table addECUTable(Composite parent) {
        Composite serviceTableComposite = new Composite(parent, SWT.NONE);
        serviceTableComposite.setLayout(new GridLayout(1, false));
        Label serviceTableLabel = new Label(serviceTableComposite, SWT.NONE);
        serviceTableLabel.setText("Services define:");
        GridData dataServiceLabel = new GridData(SWT.FILL, SWT.CENTER, true, false);
        dataServiceLabel.horizontalSpan = 1;
        serviceTableLabel.setLayoutData(dataServiceLabel);
        final Table serviceTable = new Table(serviceTableComposite, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
        serviceTable.setHeaderVisible(true);
        serviceTable.setLinesVisible(true);

        while (serviceTable.getColumnCount() > 0) {
            serviceTable.getColumns()[0].dispose();
        }

        GridData tableData = new GridData(SWT.FILL, SWT.FILL, true, true);
        tableData.heightHint = 150;
        serviceTable.setLayoutData(tableData);

        String[] columnNames = {"serviceName", "serviceID", "RPCType", "fieldPropertyName", "vechileBody", "VCC_MCU", "TMS", "VCU", "FLZCU", "FRZCU", "ICC", "ICC_8155_Android", "ICC_8155_QNX", "TBOX"};
        int[] columnWidths = {100, 60, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100};

        for (int i = 0; i < columnNames.length; i++) {
            TableColumn column = new TableColumn(serviceTable, SWT.NONE);
            column.setText(columnNames[i]);
            column.setWidth(columnWidths[i]);
        }
        // System.out.println("Number of columns after initialization: " + serviceTable.getColumnCount());

        Display.getDefault().asyncExec(() -> {
            serviceTableComposite.layout(true, true);
            int totalTableWidth = serviceTable.getClientArea().width;
            int sumOfOtherColumns = 0;
            for (int i = 0; i < serviceTable.getColumnCount() - 1; i++) {
                sumOfOtherColumns += serviceTable.getColumn(i).getWidth();
            }

            // Set the width of the last column to the remaining space.
            TableColumn lastColumn = serviceTable.getColumn(serviceTable.getColumnCount() - 1);
            int lastColumnWidth = totalTableWidth - sumOfOtherColumns - 2;
            if (lastColumnWidth > 0) {
                lastColumn.setWidth(lastColumnWidth);
            }
            TableManager.printTableTotalWidth(serviceTable);
        });

        return serviceTable;
    }

    public static Button[] addECUButton(Composite parent) {
	    Composite buttonComposite = new Composite(parent, SWT.NONE);
	    RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
	    rowLayout.spacing = 5;
	    rowLayout.marginLeft = rowLayout.marginRight = rowLayout.marginTop = rowLayout.marginBottom = 0;
	    buttonComposite.setLayout(rowLayout);

	    GridData buttonData = new GridData(SWT.END, SWT.CENTER, false, false);
	    buttonData.horizontalSpan = 2;
	    buttonComposite.setLayoutData(buttonData);

	    Button btnAdd = new Button(buttonComposite, SWT.PUSH);
	    btnAdd.setText("Add");
	    Button btnRemove = new Button(buttonComposite, SWT.PUSH);
	    btnRemove.setText("Del");

	    return new Button[]{btnAdd, btnRemove};
	}
}
